package com.hzvdvmc.autosale.sale.presenter;

import android.content.Context;
import android.util.Log;

import com.hzvdvmc.autosale.sale.bean.AdverResBean;
import com.hzvdvmc.autosale.socket.HttpConstants;
import com.hzvdvmc.autosale.sale.bean.QrCodeResponseBean;
import com.hzvdvmc.autosale.sale.bean.ShopListRespnse;
import com.hzvdvmc.autosale.sale.ui.ShopView;
import com.hzvdvmc.autosale.util.GsonUtils;
import com.hzvdvmc.autosale.util.LogUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;
import okhttp3.OkHttpClient;

/**
 * Created by william on 2017/12/14.
 */

public class MainPresenter {
    private ShopView mView;

    public MainPresenter(ShopView mView, Context mContext) {
        this.mView = mView;
    }

    public void getShopList(String machineKey, String pageSize, String pageIndex) {
        LogUtil.i("url:" + HttpConstants.getShopList()
                + ",machineId" + machineKey
                + ",pageIndex" + pageIndex
                + ",pageSize" + pageSize);
        OkHttpUtils
                .post()
                .url(HttpConstants.getShopList())
                .addParams("machineId", machineKey)
                .addParams("pageIndex", pageIndex)
                .addParams("pageSize", pageSize)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mView.onGetShopListFailure("请求错误" + e.getMessage());
                        LogUtil.i(e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("Net", "network---" + response);
                        ShopListRespnse shopListRespnse = GsonUtils.GsonToBean(response, ShopListRespnse.class);
                        if (shopListRespnse != null) {
                            mView.onGetShopListSuccess(shopListRespnse.getRetObj());
                        }
                    }
                });

    }

    public void getCodeValue(String machineId, String waresId) {
        OkHttpUtils
                .post()
                .url(HttpConstants.getQRCodeValue())
                .addParams("machineId", machineId)
                .addParams("waresId", waresId)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mView.onGetShopListFailure("请求错误");
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("Net", "network---" + response);
                        try {
                            QrCodeResponseBean bean = GsonUtils.GsonToBean(response, QrCodeResponseBean.class);
                            if (bean != null) {
                                mView.onGetQrCodeSuccess(bean);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void getAdverRes(final String machineId){
        OkHttpUtils
                .post()
                .url(HttpConstants.getAdverRes())
                .addParams("machineId", machineId)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mView.onGetAdverFailure("请求错误");
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.i("Net", "network---" + response);
                        try {
                            AdverResBean bean = GsonUtils.GsonToBean(response, AdverResBean.class);
                            if (bean != null) {
                                mView.onGetAdverSuccess(bean);
                            }
                        } catch (Exception e) {
                            mView.onGetAdverFailure("数据解析失败");
                            e.printStackTrace();
                        }
                    }
                });
    }

}
